package com.liuyfly.leetcode.questions;

import lombok.extern.slf4j.Slf4j;

/**
 * @author liuyangfang at 2022-08-22 16:40
 */
@Slf4j
public class Question0005 {
    /**
     * 给你一个字符串 s，找到 s 中最长的回文子串。 <br/>
     * <br/>
     * 链接：https://leetcode.cn/problems/longest-palindromic-substring/
     */
    public static String longestPalindrome(String s) {
        if (s.length() == 1) {
            return s;
        } else if (isPalindrome(s)) {
            return s;
        }
        String longest = "";
        for (int i = 0; i < s.length(); i++) {
            String longest2 = "";
            for (int j = s.length(); j > i; j--) {
                String sub = s.substring(i, j);
                if (isPalindrome(sub) && sub.length() > longest2.length()) {
                    longest2 = sub;
                }
            }
            if (longest2.length() > longest.length()) {
                longest = longest2;
            }
        }
        return longest;
    }

    public static boolean isPalindrome(String s) {
        int length = s.length();
        if (length <= 1) {
            return true;
        }
        int half = length / 2;
        for (int i = 0; i <= half; i++) {
            if (s.charAt(i) != s.charAt(length - i - 1)) {
                return false;
            }
        }
        return true;
    }

    /**
     * 动态规划
     */
    public static String longestPalindrome_official(String s) {
        int len = s.length();
        if (len < 2) {
            return s;
        }
        int maxLen = 1;
        int begin = 0;
        boolean[][] dp = new boolean[len][len];
        for (int i = 0; i < len; i++) {
            dp[i][i] = true;
        }
        char[] charArray = s.toCharArray();
        for (int l = 2; l <= len; l++) {
            for (int i = 0; i < len; i++) {
                int j = l + i - 1;
                if (j >= len) {
                    break;
                }
                if (charArray[i] != charArray[j]) {
                    dp[i][j] = false;
                } else {
                    if (j - i < 3) {
                        dp[i][j] = true;
                    } else {
                        dp[i][j] = dp[i + 1][j - 1];
                    }
                }
                if (dp[i][j] && j - i + 1 > maxLen) {
                    maxLen = j - i + 1;
                    begin = i;
                }
            }
        }
        return s.substring(begin, begin + maxLen);
    }

    public static void main(String[] args) {
        log.info("start");
        String sss = "asbdkasdkfjasdfjaskteeeeeeeeeetttttttttaaaaddddffffabcdefgfedcbaffffddddaaaattttttttteeeeeeeeeetdfjaksjfksjkdfja";
        StringBuilder builder = new StringBuilder();
        builder.append("bbddddddb");
        for (int i = 0; i < 2000; i++) {
            builder.append("a");
        }
        builder.append("cggggggcc");
        sss = builder.toString();
        long start = System.currentTimeMillis();
        String s1 = longestPalindrome(sss);
        long end = System.currentTimeMillis();
        log.info("result:[{}], cost:[{}]", s1, (end - start));
        log.info("cost:[{}]", (end - start));
        long start2 = System.currentTimeMillis();
        String s2 = longestPalindrome_official(sss);
        long end2 = System.currentTimeMillis();
        log.info("result:[{}], cost:[{}]", s2, (end2 - start2));
        log.info("cost:[{}]", (end2 - start2));
    }
}
